// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) ansi lnc 

package com.example.airkissdemo.core;

import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import junit.framework.Assert;

// Referenced classes of package com.example.airkissdemo.core:
//            BaseTask, IOnTaskEndCallback, TaskPriority

public abstract class BaseTaskDispatchThread
    implements Runnable
{

    public BaseTaskDispatchThread(IOnTaskEndCallback iontaskendcallback)
    {
        mIsRunning = false;
        mTaskList = new ArrayList();
        mTaskEndCallback = iontaskendcallback;
    }

    private boolean getRunningState()
    {
        mStateLock.lock();
        boolean flag = mIsRunning;
        mStateLock.unlock();
        return flag;
    }

    private void setRunningState(boolean flag)
    {
        mStateLock.lock();
        mIsRunning = flag;
        mStateLock.unlock();
    }

    public void run()
    {
        setRunningState(true);
        do
        {
            try
            {
                mAvailable.acquire();
                if(mAvailable.availablePermits() >= 10000)
                    mAvailable.notify();
            }
            catch(InterruptedException interruptedexception)
            {
                interruptedexception.printStackTrace();
            }
            if(!getRunningState())
                return;
            mTaskListLock.lock();
            BaseTask basetask = (BaseTask)mTaskList.get(0);
            mTaskListLock.unlock();
            int i = taskImpl(basetask);
            if(mTaskEndCallback != null)
                mTaskEndCallback.onTaskEnd(i, basetask);
            mTaskListLock.lock();
            if(!mTaskList.remove(basetask))
                Log.e("AirKiss.BaseTaskDispatchThread", "current task not found!");
            mTaskListLock.unlock();
        } while(true);
    }

    public void startTask(BaseTask basetask)
    {
        int i;
        Assert.assertNotNull(basetask);
        mTaskListLock.lock();
        i = 0;
_L4:
        if(i < mTaskList.size()) goto _L2; else goto _L1
_L1:
        if(i > 0 && i == mTaskList.size())
            i--;
        mTaskList.add(i, basetask);
        mTaskListLock.unlock();
        mAvailable.release();
        if(mAvailable.availablePermits() > 10000)
        {
            Log.w("AirKiss.BaseTaskDispatchThread", "Too more tasks in queque(> 10000). change unblock to block");
            mAvailable.wait();
        }
_L5:
        BaseTask basetask1;
        return;
_L2:
        if(basetask.mPriority.compareTo((basetask1 = (BaseTask)mTaskList.get(i)).mPriority) > 0) goto _L1; else goto _L3
_L3:
        i++;
          goto _L4
        InterruptedException interruptedexception;
        interruptedexception;
        Log.e("AirKiss.BaseTaskDispatchThread", "Semaphore.wait error");
        interruptedexception.printStackTrace();
          goto _L5
    }

    public void stop()
    {
        setRunningState(false);
        mAvailable.release();
    }

    protected abstract int taskImpl(BaseTask basetask);

    private static final String TAG = "AirKiss.BaseTaskDispatchThread";
    private final int MAX_AVAILABLE_PERMITS = 10000;
    private final Semaphore mAvailable = new Semaphore(0);
    private boolean mIsRunning;
    private final Lock mStateLock = new ReentrantLock();
    private IOnTaskEndCallback mTaskEndCallback;
    private List mTaskList;
    private final Lock mTaskListLock = new ReentrantLock();
}
